from django.shortcuts import render
from rest_framework.views import APIView, Response
from django.forms.models import model_to_dict
from web import models
# Create your views here.
def init_data(iscode, data, msg=''):
    if iscode:
        data = {
            'code': 0,
            'data': data,
            'msg': '成功' if msg == '' else msg
        }
        return data
    else:
        data = {
            'code': 1,
            'data': [],
            'msg': '失败' if msg == '' else msg
        }
        return data
class categoryView(APIView):
    def post(self,request):
        return Response('lijiii')
    def get(self,request):
        category_list = models.category.objects.filter()
        # 列表生成表达式
        data = [model_to_dict(category_item) for category_item in category_list]
        return Response(init_data(True,data))
